package com.zat.mes.andrmes.dofun.work.fragment

import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.google.gson.Gson
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseFragment
import com.zat.mes.andrmes.data.GetPackBean
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.dofun.work.adapter.GetPackAdapter
import com.zat.mes.andrmes.dofun.work.vm.GetPackModel
import com.zat.mes.andrmes.event.CheckInUnEventMessage
import com.zat.mes.andrmes.event.GetPackEventMessage
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.PreferencesUtil
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.activity_get_pack.*
import kotlinx.android.synthetic.main.fragment_get_pack.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @author zy
 * @date :2020-07-29 14:51
 * @description: 待领取裁片包
 */
class GetPackFragment: BaseFragment() {

    var currentPage: Int = 1;
    private var getPackModel: GetPackModel? = null
    var factoryId:Long = 0
    override fun getViewId(): Int {
        return R.layout.fragment_get_pack
    }

    override fun isOpenTitleBar(): Boolean {
        return false
    }

    override fun initView() {

    }

    override fun initData() {
        EventBus.getDefault().register(this)
        val loginResultBean = Gson().fromJson(
            PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
            LoginResultBean::class.java
        )
        factoryId = loginResultBean.factoryid
        swiperGetpack.setOnRefreshListener {
            currentPage = 1
            requestData(factoryId)
        }

        recyclerGetpack.layoutManager = LinearLayoutManager(context)
        val getPackAdapter = GetPackAdapter(R.layout.item_get_pack)
        recyclerGetpack.adapter = getPackAdapter

        val loadMoreModule = getPackAdapter.loadMoreModule
        // 当数据不满一页时，是否继续自动加载（默认为true）
        loadMoreModule?.isEnableLoadMoreIfNotFullPage = false
        loadMoreModule?.isAutoLoadMore = false
        loadMoreModule?.setOnLoadMoreListener(OnLoadMoreListener {
            requestData(factoryId)
        })

        getPackModel = ViewModelProvider(this).get(GetPackModel::class.java)

        //获取列表
        getPackModel?.getPackList()?.observe(this, Observer {
            if (currentPage == 1) {
                swiperGetpack.isRefreshing = false
            }
            if (it != null) {
                val result = it.result
                if (result != null) {
                    if (result.items != null) {
                        if (currentPage == 1) {
                            if (result.items == null || result.items.size <= 0) {
                                setEmptyVisible()
                            } else {
                                setEmptyGone()
                            }
                            getPackAdapter.setNewData(result.items as MutableList<GetPackBean>)
                        } else {
                            getPackAdapter.addData(result.items)
                        }
                    }
                    if (result.currentPage == result.totalPage) {
                        loadMoreModule?.loadMoreEnd()
                    } else {
                        loadMoreModule?.loadMoreComplete()
                        currentPage++
                    }
                } else {
                    loadMoreModule?.loadMoreEnd()
                }
            } else {
                loadMoreModule?.loadMoreEnd()
            }


        })
        //失败
        getPackModel?.getPackListFail()?.observe(this, Observer {
            if (currentPage == 1) {
                swiperGetpack.isRefreshing = false
            }
            closeLoading()
            Toast.makeText(context,it,Toast.LENGTH_SHORT).show()
        })

        requestData(factoryId)
    }

    /**
     * 选中event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onCheckInEvent(event: GetPackEventMessage) {
        currentPage = 1
        requestData(factoryId)
    }
    /**
     * 请求数据
     */
    fun requestData(factoryId: Long){
        getPackModel?.loadGetPack(currentPage, 20, factoryId)
    }

    override fun onDestroyView() {
        EventBus.getDefault().unregister(this)
        super.onDestroyView()
    }
}